package com.example.lanchat.ui.chat;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.lanchat.MainActivity;
import com.example.lanchat.R;
import com.example.lanchat.databinding.ChatFragmentBinding;
import com.example.lanchat.databinding.ConnFragmentBinding;
import com.example.lanchat.ui.conn.ConnViewModel;

public class ChatFragment extends Fragment {

    private ChatViewModel mViewModel;
    private ChatFragmentBinding binding;
    MainActivity activity;
    EditText ET;
    TextView TV;
    public static ChatFragment newInstance() {
        return new ChatFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mViewModel =
                new ViewModelProvider(this).get(ChatViewModel.class);
        binding = ChatFragmentBinding.inflate(inflater, container, false);
        activity=(MainActivity)getActivity();
        View root = binding.getRoot();
        Button send=binding.sendButton;
        ET=binding.ET;
        TV= binding.TV;
        //初始文本内容更新
        mViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                TV.setText(s);
            }
        });
        //更新显示框
        activity.myNotifyLiveData.observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                String info=TV.getText()+s;
                TV.setText(info);
            }
        });
        //获取编辑框内容并发送
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (MainActivity.isBluetooth){
                    //蓝牙链接
                    if (activity.isMaster){
                        activity.mySendWithText(ET.getText().toString());
                    }else {
                        activity.sendNotifyMSG(ET.getText().toString());
                    }
                }else {
                    //Tcp通讯
                    activity.sendTcpMSG(activity.isTCPServer,ET.getText().toString());
                }
            }
        });


        return root;
    }

    @Override
    public void onResume() {
        super.onResume();

        if (activity.isMaster){
            //如果是主机模式聊天为蓝色
          TV.setBackgroundColor(Color.argb(255,0,0,255));
        }else {
            //如果是从机模式聊天为紫色
            TV.setBackgroundColor(Color.argb(255,187,134,255));
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(ChatViewModel.class);
        // TODO: Use the ViewModel
    }

}